local SkillMove = MClass("SkillMove")

--ctor 初始化UI init设置静态数据 setData设置动态数据  updateData更新数据
function SkillMove:ctor()
    --self.root 默认为根节点
    self.bg_1 = self:findChild("bg_1")
    self.bg_2 = self:findChild("bg")
    self.arrowLeft = self:findChild("arrow_1")
    self.arrowRight = self:findChild("arrow_2")
    self.cancel = self:findChild("stop")
    
	self:init()
end

function SkillMove:init()
   self.originX = self.bg_1:getPositionX()
end

function SkillMove:setNormalKill(bNormal)
	self:setManualCancel(false)
	self:cancelCalcTime()
	self:setVisible(true)
	self.cancel:setVisible(false)
	if bNormal then
		--to so
		self.arrowLeft:setVisible(false)
		self.arrowRight:setVisible(false)
		self.bg_2:setVisible(false)
	else
		-- to do
	end
	self:startCalcTime()
end

function SkillMove:checkCancel(pos)
	if self.bManualCancel then
		return self.bManualCancel
	end
	local boundingBox = self.cancel:getBoundingBox()
	local localPos = self.root:convertToNodeSpace(pos)
	if cc.rectContainsPoint(boundingBox,localPos) then
		return true
	else
		return false
	end
end

function SkillMove:setManualCancel(bCancel)
	self.bManualCancel = bCancel
	if bCancel then
		BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SKILL_HIDEEFFECT)
		self:cancelCalcTime()
	end
end

function SkillMove:setMove(du)
	-- body
	self.bg_1:setPositionX(self.originX+du)
end

function SkillMove:setCancel()
	self.cancel:setVisible(true)
end

function SkillMove:setData(data)

end

function SkillMove:updateData(data)
	
end

function SkillMove:startCalcTime()
	if not self.timeRecord then
		self.timeRecord = {}
		self.timeRecord.startTime = app:getUTCTime()
	end

	BattleUIEvent:addObjToFrameUpdate(self,SkillMove.updateFrame)
	self.reset = false
end


function SkillMove:cancelCalcTime()
	BattleUIEvent:removeFromUpdate(self)
	self.timeRecord = nil
	self.cancel:setVisible(false)
	self.reset = true
end


function SkillMove:updateFrame()
	if not self:isVisible() or self.reset then
		self:cancelCalcTime()
	end

	if self.timeRecord and not self.timeRecord.showCancel then
		local lastTime = self.timeRecord.startTime
		local currentTime = app:getUTCTime()
		local canShow = true
		if lastTime and currentTime - lastTime < Constant.BattleCancelInterval then
			canShow = false
		end
		
		if canShow then
			self:setCancel()
			self.timeRecord.showCancel = true
		end
	end
end


return SkillMove